package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * 管理员后台
 * @author fxy
 * @since 2022-05-10
 */
@RestController
@RequestMapping("/cmsservice/banner")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    //分页查询banner
    @GetMapping("/pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable long current, @PathVariable long limit){
        Page<CrmBanner> page=new Page<>(current,limit);
        crmBannerService.page(page,null);

        long total = page.getTotal();
        List<CrmBanner> records = page.getRecords();

        return R.ok().data("records",records).data("total",total);
    }

    //添加banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);

        return R.ok();
    }

    //修改banner
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);

        return R.ok();
    }

    //删除banner
    @DeleteMapping("/daleteBanner/{id}")
    public R addBanner(@PathVariable String id){
        crmBannerService.removeById(id);

        return R.ok();
    }

    //根据id查询
    @GetMapping("/getBanner/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("crmBanner",crmBanner);
    }
}

